package cn.sunjinxin.trade.application.event;

import cn.sunjinxin.savior.core.helper.SpringHelper;
import cn.sunjinxin.savior.event.context.EventContext;
import cn.sunjinxin.savior.event.listener.sub.AsyncListener;
import cn.sunjinxin.trade.client.api.TradeService;
import cn.sunjinxin.trade.client.api.request.OrderPayRequest;
import cn.sunjinxin.trade.domain.enums.OrderType;
import cn.sunjinxin.trade.domain.enums.TocType;
import cn.sunjinxin.trade.domain.even.TradeEvent;
import cn.sunjinxin.trade.domain.model.Order;
import cn.sunjinxin.trade.domain.model.Toc;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author issavior
 */
public class CreateOrderListener implements AsyncListener<TradeEvent, Order> {

    @Override
    public List<TradeEvent> supportEventType() {
        return Lists.newArrayList(TradeEvent.CREATE);
    }

    @Override
    public void handle(EventContext<TradeEvent, Order> tradeEventOrderEventContext) {
        Order tpOrder = tradeEventOrderEventContext.getRequest();

        Toc toc = Toc.init(tpOrder, TocType.PAY_ORDER_TIME_OUT);
        toc.register();

        if (tpOrder.getOrderType() == OrderType.B2B) {
            SpringHelper.getBean(TradeService.class).pay(OrderPayRequest.to().end());
        }

//        if (tpOrder.getOrderType() == OrderType.B2P) {
//            // 初始化子履约单
//
//            // 等待所有子履约单初始化完毕
//
//            // 所有子履约单初始化完毕，开始hold单
//
//            // 开始预定资源
//            // 推主履约单
//
//        }

    }

}
